package com.lby.redisdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.lby.redisdemo.bean.MessageBean;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName RedisController
 * @Description TODO
 * @Author louby0923
 * @Date 2023/3/31 13:53
 * @Version 1.0
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping(value = "/send", produces = "application/json;charset=UTF-8")
    public String send(@RequestBody JSONObject message) {


        String[] arry = {"ZHOU", "DA", "TOU"};

        List<MessageBean> messageBeans = new ArrayList<>();
        for (String s : arry) {
            redisTemplate.convertAndSend(s, JSONObject.toJSONString(message));
            messageBeans.add(MessageBean.builder()
                    .channel(s)
                    .msg(JSONObject.toJSONString(message))
                    .build());
        }
       // mongoTemplate.insert(messageBeans);
        mongoTemplate.insertAll(messageBeans);


        return "发送成功！";
    }


    @PostMapping(value = "/send2", produces = "application/json;charset=UTF-8")
    public String send2(@RequestBody JSONObject message) {


        String[] arry = {"ZHOU", "DA", "TOU"};

        List<MessageBean> messageBeans = new ArrayList<>();
        for (String s : arry) {
            redisTemplate.convertAndSend(s, JSONObject.toJSONString(message));
            messageBeans.add(MessageBean.builder()
                    .channel(s)
                    .msg(JSONObject.toJSONString(message))
                    .build());
        }
       // mongoTemplate.insert(messageBeans);
        mongoTemplate.insertAll(messageBeans);


        return "发送成功！";
    }

}
